﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.StaticData;
using WmsApp.StaticData.Unity;

namespace WmsApp.UI
{
    public partial class QInvoiceBill : DevExpress.XtraEditors.XtraUserControl,IQueryControl
    {
        public QInvoiceBill()
        {
            InitializeComponent();

            if (GlobalClass.RunTimeMode)
            {
                try
                {
                    DataBinding.Binding.BindProvider(this.lookUpEditProvider);

                }
                catch (Exception exception)
                {
                    Exceptions.ExceptionHandler.ShowException(exception);
                }
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //重构后的代码规范，就一句话
            if (GlobalClass.RunTimeMode)
            {
                SubjectFactory.AddObserverToSubject(new CustomerObserver("customer", SubjectFactory.GetSubject(),
                                                                         this.lookUpEditProvider.Properties,
                                                                         (ICanClosing) this.FindForm()));
            }
        }

        #region Implementation of IQueryControl

        public string GetCondition()
        {
            string condition = "";
 
            if (lookUpEditProvider.EditValue != null && !String.IsNullOrEmpty(lookUpEditProvider.EditValue.ToString()))
            {
                condition += (String.Format(" and ProviderCode ='{0}'", lookUpEditProvider.EditValue));
            }
    
            if (!String.IsNullOrEmpty(this.textEditInvoiceNo.Text))
            {
                condition += (String.Format(" and InvoiceNo  like '%{0}%'", textEditInvoiceNo.Text));
            }

            #region 创建日期
            DateTime dt;
            if (this.dateEditExtDateTime1.EditValue != null && DateTime.TryParse(this.dateEditExtDateTime1.EditValue.ToString(),out dt))
            {
                condition += (String.Format(" and ExtDateTime1  >= '{0}'", this.dateEditExtDateTime1.EditValue.ToString()));
            }
            if (this.dateEditExtDateTime1_2.EditValue != null && DateTime.TryParse(this.dateEditExtDateTime1_2.EditValue.ToString(), out dt))
            {
                condition += (String.Format(" and ExtDateTime1  <= '{0}'", this.dateEditExtDateTime1_2.EditValue.ToString()));
            }
            #endregion

            #region 开票日期
            if (this.dateEditExtDateTime2.EditValue != null && DateTime.TryParse(this.dateEditExtDateTime2.EditValue.ToString(), out dt))
            {
                condition += (String.Format(" and ExtDateTime2  >= '{0}'", this.dateEditExtDateTime2.EditValue.ToString()));
            }
            if (this.dateEditExtDateTime2_2.EditValue != null && DateTime.TryParse(this.dateEditExtDateTime2_2.EditValue.ToString(), out dt))
            {
                condition += (String.Format(" and ExtDateTime2  <= '{0}'", this.dateEditExtDateTime2_2.EditValue.ToString()));
            }
            #endregion

            //发票代码

            if (!String.IsNullOrEmpty(this.textEditExtVarChar1.Text))
            {
                condition += (String.Format(" and ExtVarChar1  like '%{0}%'", textEditExtVarChar1.Text));
            }

            //发票备注

            if (!String.IsNullOrEmpty(this.textEditExtVarChar2.Text))
            {
                condition += (String.Format(" and ExtVarChar2  like '%{0}%'", textEditExtVarChar2.Text));
            }

            #region 发票状态
            if (String.IsNullOrEmpty(this.comboBoxEditInvoiceBillState.Text))
            {
                
            }
            else if (this.comboBoxEditInvoiceBillState.Text == @"部分付款")
            {
                condition += (" and  AmountTax-InStoreBillAmount > 0 and InStoreBillAmount>0");
            }
            else if (this.comboBoxEditInvoiceBillState.Text == @"未付款")
            {
                condition += (" and  InStoreBillAmount = 0");
            }
            else if (this.comboBoxEditInvoiceBillState.Text == @"全部已付款")
            {
                condition += (" and  AmountTax- InStoreBillAmount = 0");
            }
            #endregion

            return condition;
        }

        #endregion


        #region IQueryControl Members


        public void AddCustomerCondition(string condition)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
